package com.bidevalution.support.util;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import com.bidevalution.support.factory.MapFactory;

public class MapUtil  extends HashMap<String, Object> implements MapFactory {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6381352953357066461L;
	private MapUtil() {
		
	}
	public static MapFactory create() {
		return new MapUtil();
	}

	@Override
	public MapFactory putValue(String key, Object value) {
		put(key, value);
		return this;
	}
	@Override
	public Map<String, Object> getMap() {
		return (Map<String, Object>)this;
	}
	@Override
	public MapFactory putAllValue(Map<String, Object> map) {
		putAll(map);
		return this;
	}
	/**
	 * 让 Map按key进行排序
	 */
	public MapFactory sortByKey(boolean isAsc) {

		Map<String, Object> sortMap = new TreeMap<String, Object>(new Comparator<String>() {
			@Override
			public int compare(String s1, String s2) {
				if(isAsc) {
					return s1.compareTo(s2);  //从小到大排序
				}else {
					return s2.compareTo(s1);  //从大到小排序
				}
			}
		});
		sortMap.putAll(this);
		return this;
	}
}
